/**
  ******************************************************************************
  * @file    ButtonEvent.h
  * @author  FASTSHIFT
  * @version V1.0.0
  * @date    2018-12-23
  * @brief   非阻塞式按键事件驱动库，支持短摁、长摁、释放、状态改变事件的识别
  ******************************************************************************
  * @attention
  * 需要提供一个精确到毫秒级的系统时钟，在ButtonEvent.cpp里定义ButtonEvent_Millis
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __BUTTONEVENT_H
#define __BUTTONEVENT_H

#include "stdint.h"

class ButtonEvent {
    typedef void(*ButtonEvent_FuncCallBack_t)(void);
public:
    ButtonEvent(bool NoPressState = 0, uint16_t LongPressTimeMs_Set = 500);
    void EventAttach_Press(ButtonEvent_FuncCallBack_t Function);
    void EventAttach_LongPress(ButtonEvent_FuncCallBack_t Function);
    void EventAttach_Release(ButtonEvent_FuncCallBack_t Function);
    void EventAttach_Change(ButtonEvent_FuncCallBack_t Function);
    void EventMonitor(uint8_t NowState);

    typedef enum {NoPress, Press, LongPress} ButtonState_Type;
    operator uint8_t() {
        return Button_NowState;
    };
private:
    bool Button_NoPressState;
    uint8_t Button_NowState;
    uint16_t LongPressTimeMs;
    uint32_t PressKey_TimePoint;
    bool IS_Pressed;

    ButtonEvent_FuncCallBack_t ButtonPress_Function;
    ButtonEvent_FuncCallBack_t ButtonLongPress_Function;
    ButtonEvent_FuncCallBack_t ButtonRelease_Function;
    ButtonEvent_FuncCallBack_t ButtonChange_Function;
};

#endif
